_im); $h = imagesy($p->_im); $this->_im = imagecreatetruecolor($w, $h); if(!$this->_im)exit('create failed'); if(!imagecopy($this->_im, $p->_im, 0, 0, 0, 0, $w, $h))exit('copy failed'); $this->_type=$p->_type; $this->_width=$p->_width; $this->_height=$p->_height; $this->_path=$p->_path; return 1; } $this->_im=false;$this->_type='';$this->_width=0;$this->_height=0;$this->_path=''; if(!is_file($p))return 0;if(false===($fp=@fopen($p,'rb')))return 0;$h=fread($fp,8);fclose($fp); if($h=="\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"){$this->_im=imagecreatefrompng($p);$this->_type='png';} elseif(substr($h,0,2)=="\xFF\xD8"){$this->_im=imagecreatefromjpeg($p);$this->_type='jpeg';} elseif(substr($h,0,3)=='GIF'){$this->_im=imagecreatefromgif($p);$this->_type='gif';} else return 0; $this->_width=imagesx($this->_im); $this->_height=imagesy($this->_im); $this->_path=$p; return 1; } // display(void) function display() { header('Content-disposition: inline;filename="'.basename($this->_path).'"'); header('Content-type: image/'.$this->_type); switch($this->_type){case'png':imagepng($this->_im);break;case'jpeg':imagejpeg($this->_im,null,85);break;case'gif':imagegif($this->_im);break;} } // resizeTo(width, height [,keep ratio] [,crop to specified dimension if keep ratio is set]) function resizeTo ( $new_width, $new_height, $keep_ratio = true, $crop = false ) { $original_width = imagesx ( $this->_im ); $original_height = imagesy ( $this->_im ); $original_ratio = $original_width / $original_height; $new_ratio = $new_width / $new_height; // parameters for imagecopyresamped $src_x = 0; $src_y = 0; $src_w = $original_width; $src_h = $original_height; if ( $keep_ratio ) { if ( $original_ratio >= 1 ) // WIDE or SQUARE { $resized_new_width = $new_height * $original_ratio; if ( $resized_new_width > $new_width ) { if ( $crop ) { $original_resized_width = $original_height * $new_ratio; $width_diff = $original_width - $original_resized_width; $src_x = $width_diff / 2; $src_w = $original_resized_width; } else $new_height = $new_width / $original_ratio; } else $new_width = $resized_new_width; } else // TALL { $resized_new_height = $new_width / $original_ratio; if ( $resized_new_height > $new_height ) { if ( $crop ) { $original_resized_height = $original_width / $new_ratio; $height_diff = $original_height - $original_resized_height; $src_y = floor ( $height_diff / 2 ); $src_h = $original_height - ceil ( $height_diff ); } else $new_width = $new_height * $original_ratio; } else $new_height = $resized_new_height; } } $new_im = imagecreatetruecolor ( ceil($new_width), ceil($new_height) ); if ( !$new_im ) { print "NW: $new_width, NH: $new_height"; return false; } imagecopyresampled ( $new_im, $this->_im, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h ); imagedestroy ( $this->_im ); $this->_im = &$new_im; } // horizontal anchor: left, center, right // vertical anchor: top, center, bottom function crop ( $width, $height, $horizontal_anchor = 'center', $vertical_anchor = 'center' ) { $original_width = imagesx ( $this->_im ); $original_height = imagesy ( $this->_im ); $width_diff = $original_width - $width; $height_diff = $original_height - $height; switch ( $horizontal_anchor ) { default: case 'left': $src_x = 0; break; case 'center': $src_x = $width_diff / 2; break; case 'right': $src_x = $width_diff; break; } switch ( $vertical_anchor ) { default: case 'top': $src_y = 0; break; case 'center': $src_y = $height_diff / 2; break; case 'bottom': $src_y = $height_diff; break; } $new_im = imagecreatetruecolor ( $width, $height ); imagecopy ( $new_im, $this->_im, 0, 0, $src_x, $src_y, $width, $height ); imagedestroy ( $this->_im ); $this->_im = &$new_im; } // addInfo([show size][,show original image dimension]) function addInfo($s=1, $d=1) { $w=imagesx($this->_im); $h=imagesy($this->_im); $font = 2; if($s&&$d) $str=sprintf("%dx%d - %s",$this->_width,$this->_height,$this->_getsize(filesize($this->_path))); elseif($s) $str = $this->_getsize(filesize($this->_path)); elseif($d) $str = sprintf("%dx%d", $this->_width, $this->_height); $strw=imagefontwidth($font)*strlen($str); if($strw>=$w) { $str=sprintf("%dx%d",$this->_width,$this->_height); $strw=imagefontwidth($font)*strlen($str); } $white=imagecolorallocate($this->_im,255,255,255); $black=function_exists('imagecolorallocatealpha')?imagecolorallocatealpha($this->_im,0,0,0,80):imagecolorallocate($this->_im,0,0,0); imagefilledrectangle($this->_im,0,$h-imagefontheight($font),$w,$h,$black); imagestring($this->_im,$font,($w-$strw)/2,$h-imagefontheight($font),$str,$white); } // export(destination path) function export($d, $jpeg_quality = 80) { $r=0; switch($this->_type) { case'png':$r=imagepng($this->_im,$d);break; case'jpeg':$r=imagejpeg($this->_im,$d, $jpeg_quality);break; case'gif':$r=imagegif($this->_im,$d);break; default: exit('Unknown type'); } return$r; } // watermark(watermark PNG image path[, horizontal position(left,center,right)][,vertical position(top,center,bottom)]) function watermark ( $wm, $wo = 'center', $ho = 'bottom' ) { if($this->_type=='gif')return 1; $w=imagesx($this->_im); $h=imagesy($this->_im); $inf_wm=getimagesize($wm); if(is_array($inf_wm)) { if ( $inf_wm[0] >= $this->_width || $inf_wm[1] >= $this->_height ) return 1; $im_wm=imagecreatefrompng($wm); if(!$im_wm)return 0; switch ( $wo ) { case 'left': $x = 0; break; case 'right': $x = $w - $inf_wm[0]; break; case 'center':default: $x = ($w - $inf_wm[0]) / 2; break; } switch ( $ho ) { case 'top': $y = 0; break; case 'bottom': $y = $h - $inf_wm[1]; break; case 'center': default: $y = ($h - $inf_wm[1]) / 2; } $r=imagecopy($this->_im,$im_wm,$x,$y,0,0,$inf_wm[0],$inf_wm[1]); imagedestroy($im_wm); return $r; } return 0; } // destroy(void) function destroy() { @imagedestroy($this->_im); $this->_im=null; } // returns image dimesion function getDimension() { return array ( imagesx ( $this->_im ), imagesy ( $this->_im ) ); } // _getsize ( size in byte [, unit if size is not in byte] ) function _getsize($s,$u='B') { while($s>1024) { $s/=1024; switch($u) { case'B':$u='KB';break; case'KB':$u='MB';break; case'MB':$u='GB';break; case'GB':$u='TB';break; } } return (int)$s.$u; } } endif; ?>